clear

// VDFA

insheet using data/vdfa_proc.txt
// first two lines are noise
/*
drop in 1/2
rename v1 trad_comm
rename v2 prod_comm
rename v3 reg
rename v4 value
destring value, replace
*/
save data/vdfa.dta, replace

use data/vdfa.dta, clear

// drop payments to capital goods
drop if prod_comm=="CGDS"

rename trad_comm indcode_string
merge m:1 indcode_string using data/industrydesc.dta
assert _merge==3
drop _merge
sort indcode
sencode indcode_desc, generate(upstream)
assert upstream==indcode
drop indcode indcode_string indcode_desc 

rename prod_comm indcode_string
merge m:1 indcode_string using data/industrydesc.dta
assert _merge==3
drop _merge
sort indcode
sencode indcode_desc, generate(downstream)
assert downstream==indcode
drop indcode indcode_string indcode_desc 

rename reg countrycode_string
replace countrycode_string =upper(countrycode_string)
merge m:1 countrycode_string using data/countrydesc.dta
assert _merge==3
drop _merge countryname 

save data/vdfa.dta, replace

// get rid of what we dont need
drop if isNotReallyACountry==1

// gross output
// VOA

clear
insheet using data/voa_proc.txt
// first two lines are noise

rename nsav_comm indcode_string
drop if indcode_string=="Land"
drop if indcode_string=="UnSkLab"
drop if indcode_string=="SkLab"
drop if indcode_string=="Capital"
drop if indcode_string=="NatRes"
drop if indcode_string=="CGDS"
merge m:1 indcode_string using data/industrydesc.dta
assert _merge==3
drop _merge
sort indcode
sencode indcode_desc, generate(upstream) // its not really upstream
// we just call it that way for the merge
assert upstream==indcode
drop indcode indcode_string indcode_desc

rename reg countrycode_string
replace countrycode_string =upper(countrycode_string)
merge m:1 countrycode_string using data/countrydesc.dta
assert _merge==3
drop _merge countryname 
drop if isNotReallyACountry==1
drop isNotReallyACountry countrycode_string gtapcountrycode
rename upstream downstream

save data/voa.dta, replace

// VIFA

clear
insheet using data/vifa_proc.txt
// drop payments to capital goods
drop if prod_comm=="CGDS"

rename trad_comm indcode_string
merge m:1 indcode_string using data/industrydesc.dta
assert _merge==3
drop _merge
sort indcode
sencode indcode_desc, generate(upstream)
assert upstream==indcode
drop indcode indcode_string indcode_desc 

rename prod_comm indcode_string
merge m:1 indcode_string using data/industrydesc.dta
assert _merge==3
drop _merge
sort indcode
sencode indcode_desc, generate(downstream)
assert downstream==indcode
drop indcode indcode_string indcode_desc 

rename reg countrycode_string
replace countrycode_string =upper(countrycode_string)
merge m:1 countrycode_string using data/countrydesc.dta
assert _merge==3
drop _merge countryname 

drop if isNotReallyACountry==1

save data/vifa.dta, replace

